package com.budwk.app.sqfwDoc.codeGeneratorV2.excelService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfwDoc.bo.ExcelCreateConfig;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.AbstractCustomerPdfCreateField;
import com.budwk.app.sqfwDoc.codeGeneratorV2.capacity.AbstractCreateExcelCapacity;
import com.budwk.app.sqfwDoc.codeGeneratorV2.config.ExcelGeneratorConfig;
import com.budwk.app.sqfwDoc.codeGeneratorV2.constants.CodeGeneratorConstants;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.*;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.ResultOfExcelGenerator;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.google.common.collect.Lists;
import org.nutz.dao.Cnd;

import java.util.List;

/**
 * @author ZY.Wang
 * @since 2024/6/6
 */
public abstract class Sqfw2ExcelService extends AbstractCreateExcelCapacity {

    public List<ResultOfExcelGenerator> createExcel(Sqfw2ExcelGPForController paramForController) {
        List<ExcelRowDataParam> excelRowDataParamList = getExcelRowData(paramForController);
        List<ExcelCreateConfig> excelCreateConfigList = paramForController.getExcelCreateConfigList();
        String userId = paramForController.getUserId();
        return super.generateExcel(excelCreateConfigList, getDownloadUserParam(userId), excelRowDataParamList, CodeGeneratorConstants.CLIENT_TYPE_APP);
    }

    /**
     * 为后端创建excel
     *
     * @param paramForController excel创建从controller传过来的数据
     * @return 结果
     */
    public List<ResultOfExcelGenerator> createExcelForAdmin(CompleteFlowExcelParamForController paramForController) {
        List<ExcelRowDataParam> excelRowDataParamList = getAdminExcelRowData(paramForController);
        List<ExcelCreateConfig> excelCreateConfigList = paramForController.getExcelCreateConfigList();
        String userId = paramForController.getUserId();
        return super.generateExcel(excelCreateConfigList, getDownloadUserParam(userId), excelRowDataParamList, CodeGeneratorConstants.CLIENT_TYPE_ADMIN);
    }

    /**
     * app根据条件查询数据
     *
     * @param paramForController excel创建从前端传过来的数据
     * @return 生成excel的行数据
     */
    protected List<ExcelRowDataParam> getExcelRowData(Sqfw2ExcelGPForController paramForController) {
        List<ExcelRowDataParam> resultList = Lists.newArrayList();
        List<String> formSummaryIdList = paramForController.getFormSummaryIdList();

        if (CollectionUtil.isEmpty(formSummaryIdList)) return resultList;

        SqfwFormsummaryNewService sqfwFormsummaryNewService = NutzIocUtils.getBean(SqfwFormsummaryNewService.class);
        Cnd cnd = Cnd.NEW();
        cnd.and(Sqfw_formSummary_new::getId, "in", formSummaryIdList);
        cnd.asc(Sqfw_formSummary_new::getFillDate);
        List<Sqfw_formSummary_new> formSummaryNewList = sqfwFormsummaryNewService.query(cnd);

        return transformFormSummaryToExcelRowData(formSummaryNewList);
    }

    /**
     * 后台获取生成excel数据的行数据
     *
     * @param paramForController 生成excel从前端传过来的数据
     * @return 结果
     */
    protected List<ExcelRowDataParam> getAdminExcelRowData(CompleteFlowExcelParamForController paramForController) {
        List<String> formSummaryIdList = paramForController.getFormSummaryIdList();
        if (formSummaryIdList == null || formSummaryIdList.size() == 0) return Lists.newArrayList();
        SqfwFormsummaryNewService sqfwFormsummaryNewService = NutzIocUtils.getBean(SqfwFormsummaryNewService.class);
        Cnd cnd = Cnd.NEW();
        cnd.and(Sqfw_formSummary_new::getId, "in", formSummaryIdList);
        cnd.asc(Sqfw_formSummary_new::getFillDate);
        List<Sqfw_formSummary_new> formSummaryNewList = sqfwFormsummaryNewService.query(cnd);

        return transformFormSummaryToExcelRowData(formSummaryNewList);
    }

    /**
     * 通过表单数据创建excel生成所需要的数据
     *
     * @param formSummaryNewList 表单数据
     * @return 结果
     */
    public List<ExcelRowDataParam> transformFormSummaryToExcelRowData(List<Sqfw_formSummary_new> formSummaryNewList) {
        List<ExcelRowDataParam> resultList = Lists.newArrayList();
        for (Sqfw_formSummary_new formSummaryNew : formSummaryNewList) {
            ExcelRowDataParam rowDataParam = new ExcelRowDataParam();

            CommonParam commonParam = new CommonParam();
            BeanUtil.copyProperties(formSummaryNew, commonParam);
            commonParam.setUnitName("");    //TODO
            commonParam.setLevelCityUnitName(formSummaryNew.getUnitName());

            CompleteFlowBusinessParam businessParam = new CompleteFlowBusinessParam();
            businessParam.setCompleteFlowId(formSummaryNew.getSqfwFlowId());
            businessParam.setSqfwId(formSummaryNew.getSqfwId());
            businessParam.setFillDate(formSummaryNew.getFillDate());

            AbstractCustomerPdfCreateField createExcelField = getAbstractCustomerField(formSummaryNew.getParamData());

            rowDataParam.setCreateUserCommonParam(commonParam);
            rowDataParam.setBusinessParam(businessParam);
            rowDataParam.setAbstractCustomerPdfCreateField(createExcelField);
            resultList.add(rowDataParam);

            customerPopularExcelRowDataParam(rowDataParam, formSummaryNew);
        }
        return resultList;
    }

    @Override
    public abstract List<ExcelGeneratorConfig> getExcelGeneratorConfig(List<ExcelCreateConfig> excelCreateConfigList);

    @Override
    protected void customerPopularExcelGenerateParam(ExcelGenerateParam excelGenerateParam) {

    }

    /**
     * 在创建生成参数的时候允许自定义填充数据
     *
     * @param formSummaryNew 表单数据
     */
    public void customerPopularExcelRowDataParam(ExcelRowDataParam rowDataParam, Sqfw_formSummary_new formSummaryNew) {

    }

    public abstract AbstractCustomerPdfCreateField getAbstractCustomerField(String jsonStr);
}
